Coverage Report

Created: 2024-12-19 06:34

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
D:\a\tools.proto\tools.proto\compiler\src\gen\template\loader.rs
Line
Count
Source
1
// Copyright (c) 2024, BlockProject 3D
2
//
3
// All rights reserved.
4
//
5
// Redistribution and use in source and binary forms, with or without modification,
6
// are permitted provided that the following conditions are met:
7
//
8
//     * Redistributions of source code must retain the above copyright notice,
9
//       this list of conditions and the following disclaimer.
10
//     * Redistributions in binary form must reproduce the above copyright notice,
11
//       this list of conditions and the following disclaimer in the documentation
12
//       and/or other materials provided with the distribution.
13
//     * Neither the name of BlockProject 3D nor the names of its contributors
14
//       may be used to endorse or promote products derived from this software
15
//       without specific prior written permission.
16
//
17
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
21
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22
// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
24
// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
25
// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
26
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
29
use crate::gen::template::Template;
30
use bp3d_util::simple_error;
31
use std::collections::HashMap;
32
use std::path::Path;
33
use crate::gen::codec::CodecMap;
34
35
simple_error! {
36
    pub Error {
37
        Io(std::io::Error) => "io error: {}",
38
        NotFound(String) => "template not found: {}",
39
        Compiler(crate::gen::template::Error) => "compiler error: {}"
40
    }
41
}
42
43
pub struct TemplateLoader<'a> {
44
    paths: Vec<&'a Path>,
45
    templates: HashMap<String, String>,
46
}
47
48
impl Default for TemplateLoader<'_> {
49
0
    fn default() -> Self {
50
0
        Self::new()
51
0
    }
52
}
53
54
impl<'a> TemplateLoader<'a> {
55
2
    pub fn new() -> Self {
56
2
        Self {
57
2
            paths: Vec::new(),
58
2
            templates: HashMap::new(),
59
2
        }
60
2
    }
61
62
2
    pub fn add_search_path(&mut self, path: &'a Path) {
63
2
        self.paths.push(path);
64
2
    }
65
66
4
    pub fn load(&mut self, name: String) -> Result<(), Error> {
67
4
        let file_name = name.clone() + ".template";
68
4
        for path in &self.paths {
69
4
            let path = path.join(&file_name);
70
4
            if path.exists() {
  Branch (70:16): [True: 4, False: 0]
  Branch (70:16): [Folded - Ignored]
71
4
                let data = std::fs::read_to_string(path).map_err(Error::Io)
?0
;
72
4
                self.templates.insert(name, data);
73
4
                return Ok(());
74
0
            }
75
        }
76
0
        Err(Error::NotFound(file_name))
77
4
    }
78
79
2
    pub fn compile<'fragment>(&'fragment self, name: &str, codecs: &CodecMap<'fragment, '_>) -> Result<Template<'fragment, '_>, Error> {
80
2
        let template_code = self.templates.get(name).ok_or_else(|| 
Error::NotFound(name.into())0
)
?0
;
81
2
        Template::compile_with_includes(template_code.as_bytes(), codecs).map_err(Error::Compiler)
82
2
    }
83
}